package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.mapper.*;
import com.bwie.pojo.*;
import com.bwie.utils.R;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.service.RepaisService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: chen
 * @Date:  2025/8/30 13:57
 * @Description: 
 **/

@Service
public class RepaisServiceImpl extends ServiceImpl<RepaisMapper, Repais> implements RepaisService{
    @Autowired
    private RepaisMapper mapper;

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private AreaMapper areaMapper;

    //文件上传
    @Autowired
    private FastFileStorageClient fileStorageClient;

    @Autowired
    private ImgsMapper imgsMapper;

    @Autowired
    private RecourdMapper recourdMapper;

    @Override
    public R getWeekCount() {



            //查询数据
            //List<RepaisDataVo> list =  mapper.getWeekCount();
            List<RepaisDataVo> list = mapper.getWeekCount();
            ArrayList<String> x = new ArrayList<>();
            ArrayList<Integer> y = new ArrayList<>();
            for (RepaisDataVo repaisDataVo : list) {
                x.add(repaisDataVo.getX());
                y.add(repaisDataVo.getY());
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("x",x);
            map.put("y",y);

            return R.OK(map);


    }

    @Override
    public R getYearData() {

        //获取x轴的数据  日期
        List<String> x =  mapper.selectMath();

        //获取每个状态的每个月的数量占比’
        List<Integer> y1 =  mapper.selectMonthData(0);
        List<Integer> y2 =  mapper.selectMonthData(1);
        List<Integer> y3 =  mapper.selectMonthData(2);
        List<Integer> y4 =  mapper.selectMonthData(3);

        //封装数据‘
        HashMap<String, Object> map = new HashMap<>();

        map.put("x",x);
        map.put("y1",y1);
        map.put("y2",y2);
        map.put("y3",y3);
        map.put("y4",y4);
        return R.OK(map);
    }

    @Override
    public R getList(Repais repais) {

        //分页构造
        Page<Repais> page = new Page<>(repais.getPageNum(), repais.getPageSize());
        //自定义sql
        Page<Repais> page1 = mapper.selectRepaisList(page,repais);

        for (Repais record : page1.getRecords()) {
            List<Imgs> imgs = imgsMapper.selectList(new QueryWrapper<Imgs>().
                    lambda().eq(Imgs::getRepaisId, record.getRepaisId()));
            record.setImgs(imgs);
        }


        //返回数据
        return R.OK(page1);
    }

    @Override
    public R getRepaisType() {
        List<Type> types = typeMapper.selectList(null);

        return R.OK(types);

    }

    @Override
    public R addRepais(Repais repais) {

        //获取报修人(登录用户)
        String token = request.getHeader("token");
        Integer userId = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody().get("userId", Integer.class);

        repais.setUserId(userId);
        repais.setCreateTime(new Date());
        repais.setRepaisStatus(0);
        repais.setRepaisCode(RandomUtil.randomNumbers(6));
        mapper.insert(repais);

        //报修单添加成功之后，再去添加图片(因为需要用到保修单的ID 保修单的Id是自增的)
        for (Imgs img : repais.getImgs()) {
            img.setRepaisId(repais.getRepaisId());
            imgsMapper.insert(img);
        }

        //添加报修之后 添加报修记录
        Recourd recourd = new Recourd();
        recourd.setRepaisId(repais.getRepaisId());
        recourd.setRepaisStatus(0);
        recourd.setCreateTime(new Date());
        recourd.setUserId(userId);
        recourdMapper.insert(recourd);
        return R.OK();
    }

    @Override
    public R getRepaisArea() {
        List<Area> areaList = areaMapper.selectList(null);
        return R.OK(areaList);

    }

    @Override
    public R uploadFile(MultipartFile file) {
//文件上传
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());

            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);

            String path = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(path);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }
    }

    @Override
    public R getRepaisRecord(Integer id) {

//        List<Recourd> recourds = recourdMapper.selectList(
//                new QueryWrapper<Recourd>().lambda().eq(Recourd::getRepaisId, id)
//        );

        List<Recourd> list = recourdMapper.selectListById(id);
        return R.OK(list);
    }

    @Override
    public R updateRepais(Repais repais) {

        //是否存在
        Repais repais1 = mapper.selectById(repais.getRepaisId());

        if(repais1 == null){
            return R.ERROR(500,"报修单不存在 ");
        }

        //修改数据
        repais.setCreateTime(new Date());
        mapper.updateById(repais);
        return R.OK();
    }

    @Override
    public R getControlList(Repais repais) {

        //分页构造
        Page<Repais> page = new Page<>(repais.getPageNum(), repais.getPageSize());
        //自定义sql
        Page<Repais> page1 = mapper.getControlList(page,repais);

        for (Repais record : page1.getRecords()) {
            List<Imgs> imgs = imgsMapper.selectList(new QueryWrapper<Imgs>().
                    lambda().eq(Imgs::getRepaisId, record.getRepaisId()));
            record.setImgs(imgs);
        }

        return R.OK(page1);
    }

    @Override
    public R updateRespaisStatus(Repais repais) {

        //修改状态
        repais.setCreateTime(new Date());
            mapper.updateById(repais);

            //同步维修进度表
        //添加报修之后 添加报修记录
        Recourd recourd = new Recourd();
        recourd.setRepaisId(repais.getRepaisId());
        recourd.setRepaisStatus(repais.getRepaisStatus());
        recourd.setCreateTime(new Date());
        recourd.setUserId(repais.getUserId());
        //因为状态为处理不派单的时候  getRepaisUserId是为null的
        if(repais.getRepaisUserId() != null){
            recourd.setRepaisUserId(repais.getRepaisUserId());
        }

        recourdMapper.insert(recourd);
        return R.OK();
    }



}



























